package com.sghj.record.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sghj.config.ReturnResult;
import com.sghj.record.entity.dto.RecordIncallQuery;
import com.sghj.record.entity.dto.RecordOutcallQuery;
import com.sghj.record.entity.vo.RecordIncallRes;
import com.sghj.record.entity.vo.RecordOutcallRes;
import com.sghj.record.service.RecordIncallService;
import com.sghj.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhongkai
 * @since 2024-07-15
 */
@RestController
@RequestMapping("/record-incall")
public class RecordIncallController {

    @Resource
    private RecordIncallService recordIncallService;

    /*
     * 分页条件查询呼入记录
     * */
    @PostMapping("getRecordIncallPage")
    @ApiOperation(value = "分页条件查询呼入记录")
    private ReturnResult getRecordIncallPage(@RequestBody RecordIncallQuery query) {
        if (query == null || query.getCurrent() == null || query.getLimit() == null) {
            return ReturnResult.failed().message("参数为空");
        }
        //创建分页
        Page<RecordIncallRes> page = new Page<>(query.getCurrent(), query.getLimit());
        IPage<RecordIncallRes> numPage = recordIncallService.getRecordIncallPage(query, page);
        return ReturnResult.success().data("list", numPage.getRecords()).data("total", numPage.getTotal()).message("查询成功");
    }

    /*
     * 根据计划号查询呼入记录
     * */
    @PostMapping("getRecordIncallByDayPlanNumber")
    @ApiOperation(value = "根据计划号查询呼入记录")
    private ReturnResult getRecordIncallByDayPlanNumber(@RequestBody RecordIncallQuery query) {
        List<RecordIncallRes> recordList = recordIncallService.getRecordIncallByDayPlanNumber(query.getDayPlanNumber());
        return ReturnResult.success().data("res", recordList);
    }

    /*
     * 导出回电记录
     * */
    @PostMapping("exportRecordIncall")
    @ApiOperation(value = "导出回电记录")
    private void exportRecordOutcall(HttpServletResponse response, @RequestBody RecordIncallQuery query) {

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
        //进行记录查询
        List<RecordIncallRes> recordIncallList = recordIncallService.getRecordIncallList(query);
        String sheetName = "回电记录明细表"+sdf1.format(new Date());
        String[] title = {"序号", "日计划号", "通话按键结果", "线别", "地点", "施工项目", "施工开始时间", "施工截止时间", "盯控人", "盯控人电话", "回电电话", "回电时间"};
        ExcelUtils.exportRecordIncall(response, sheetName, recordIncallList, query, title);
    }
}

